package com.ruoyi.gas.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gas.domain.GetInRecord;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gas.domain.FullfillRecord;
import com.ruoyi.gas.service.IFullfillRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * Controller
 *
 * @author h w
 * @date 2022-12-10
 */
@RestController
@RequestMapping("/gas/fullfill")
public class FullfillRecordController extends BaseController
{
    @Autowired
    private IFullfillRecordService fullfillRecordService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('gas:fullfill:list')")
    @GetMapping("/list")
    public TableDataInfo list(FullfillRecord fullfillRecord)
    {
        startPage();
        List<FullfillRecord> list = fullfillRecordService.selectFullfillRecordList(fullfillRecord);
        return getDataTable(list);
    }

    @GetMapping("/todayTotal")
    public FullfillRecord todayTotal()
    {
        FullfillRecord res = fullfillRecordService.todayTotal();
        return res;
    }


    /**
     * 查询最大充装编号
     */
    @GetMapping("/getMaxFullfillNum")
    public String getMaxFullfillNum()
    {
        return fullfillRecordService.selectMaxFullfillNum();
    }

    /**
     * 查询列表
     */
//    @PreAuthorize("@ss.hasPermi('gas:get_in:recentList')")
    @GetMapping("/recentList")
    public List<FullfillRecord> recentList(FullfillRecord fullfillRecord)
    {
        return fullfillRecordService.selectFullfillRecordRecentList(fullfillRecord);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('gas:fullfill:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FullfillRecord fullfillRecord)
    {
        List<FullfillRecord> list = fullfillRecordService.selectFullfillRecordList(fullfillRecord);
        ExcelUtil<FullfillRecord> util = new ExcelUtil<FullfillRecord>(FullfillRecord.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('gas:fullfill:query')")
    @GetMapping(value = "/{fullfillId}")
    public AjaxResult getInfo(@PathVariable("fullfillId") Long fullfillId)
    {
        return success(fullfillRecordService.selectFullfillRecordByFullfillId(fullfillId));
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('gas:fullfill:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FullfillRecord fullfillRecord)
    {
        return toAjax(fullfillRecordService.insertFullfillRecord(fullfillRecord));
    }

    /**
     * 修改
     */
    @PreAuthorize("@ss.hasPermi('gas:fullfill:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FullfillRecord fullfillRecord) throws Exception {
        fullfillRecord.setUpdateBy(getLoginUser().getUsername());
        int res = fullfillRecordService.updateFullfillRecord(fullfillRecord);
        if(res == 0) {
            return error("充装车辆所属单位余额不足，不能进行核销！");
        }
        return toAjax(res);
    }

    /**
     * 删除
     */
    @PreAuthorize("@ss.hasPermi('gas:fullfill:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fullfillIds}")
    public AjaxResult remove(@PathVariable Long[] fullfillIds)
    {
        return toAjax(fullfillRecordService.deleteFullfillRecordByFullfillIds(fullfillIds));
    }
}
